const Controller = require('egg').Controller;

class CaseController extends Controller {
  /**
     * 
     * @api {POST} /api/case/create 创建案例
     * @apiName 创建案例
     * @apiGroup 案例模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {String} cover 案例封面
     * @apiParam  {String} name  案例名称
     * @apiParam  {String} sketch 案例描述
     * @apiParam  {String} content 案例内容
     * @apiParam  {Number} characterId 人物id
     * @apiParam  {Array} classifyArr 分类数组
     * 
     * 
     * @apiParamExample  {type} Request-Example:
        {
            "cover":"https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
            "name":"case",
            "sketch":"sketch",
            "content":"content",
            "classifyArr":[1,3],
            "characterId":3
        }
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {   
                "id": 5,    //案例id
                "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg", //案例封面图
                "name": "case", //案例名
                "sketch": "sketch", //案例描述
                "content": "content",   //案例内容
                "characterId": "3", //人物id
                "projectId": 1, //所属项目id
                "updatedAt": "2021-11-11T10:53:08.272Z",
                "createdAt": "2021-11-11T10:53:08.272Z"
            }
        }
     * 
     * 
     */
  async create() {
    const { app, ctx, service } = this;
    let checkRes = new ctx.helper.checkData(
      this,
      'cover',
      'name',
      'sketch',
      'content',
      'characterId',
      'classifyArr'
    ).checkDataRes;
    try {
      if (checkRes) {
        let res = await service.web.case.create();
        if (res == 'characterId不存在') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
     * 
     * @api {DELETE} /api/case/delete 删除案例
     * @apiName 删除案例
     * @apiGroup 案例模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {Number} id 案例id
     *  
     * @apiParamExample  {type} Request-Example:
        {
            "id":5
        }
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "删除成功",
            "data": []
        }
     * 
     * 
     */
  async delete() {
    const { app, ctx, service } = this;
    try {
      let res = await service.web.case.delete();
      if (res == 'id不存在,删除失败') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }

  /**
    * 
    * @api {PUT} /api/case/update 修改案例
    * @apiName 修改案例
    * @apiGroup 案例模块
    * @apiVersion  1.0.0
    * 
    * 
    * @apiParam  {Number} id 案例id
    * @apiParam  {String} cover 案例封面
    * @apiParam  {String} name  案例名称
    * @apiParam  {String} sketch 案例描述
    * @apiParam  {String} content 案例内容
    * @apiParam  {Number} characterId 人物id
    * @apiParam  {Array} classifyArr 分类数组
    * 
    * @apiParamExample  {type} Request-Example:
       {
           "id":4
           "cover":"https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
           "name":"case",
           "sketch":"sketch",
           "content":"content",
           "classifyArr":[1],
           "characterId":3
       }
    * 
    * 
    * @apiSuccessExample {type} Success-Response:
       {
           "code": 200,
           "msg": "修改成功",
           "data": []
       }
    * 
    * 
    */
  async update() {
    const { app, ctx, service } = this;
    let checkRes = new ctx.helper.checkData(
      this,
      'id',
      'cover',
      'name',
      'sketch',
      'content',
      'characterId',
      'classifyArr'
    ).checkDataRes;

    try {
      if (checkRes) {
        let res = await service.web.case.update();
        if (res == 'characterId不存在') {
          return ctx.helper.errorModule(res);
        }
        if (res == 'id不存在,修改失败') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }

  /**
     * 
     * @api {GET} /api/case/getAll?limit=10^offset=0 获取所有案例
     * @apiName 获取所有案例
     * @apiGroup 案例模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {Number} limit 案例返回数
     * @apiParam  {Number} offset 案例偏移数
     * 
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 3,     //案例总数
                "rows": [
                    {
                        "id": 2,    //案例id
                        "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg", //案例封面图
                        "name": "case2",    //案例名
                        "sketch": "sketch2",    //案例描述
                        "content": "content2",  //案例内容
                        "projectId": 1,     //项目id
                        "createdAt": "2021-11-11T04:04:05.000Z",
                        "updatedAt": "2021-11-11T04:08:50.000Z"
                    },
                    {
                        "id": 3,
                        "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
                        "name": "case",
                        "sketch": "sketch",
                        "content": "content",
                        "projectId": 1,
                        "createdAt": "2021-11-11T04:14:26.000Z",
                        "updatedAt": "2021-11-11T04:14:26.000Z"
                    },
                    {
                        "id": 4,
                        "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
                        "name": "case",
                        "sketch": "sketch",
                        "content": "content",
                        "projectId": 1,
                        "createdAt": "2021-11-11T04:14:27.000Z",
                        "updatedAt": "2021-11-11T10:57:41.000Z"
                    }
                ]
            }
        }
     * 
     * 
     */
  async getAll() {
    const { app, ctx, service } = this;
    let res = await service.web.case.getAll();
    return ctx.helper.successModule(res);
  }

  /**
     * 
     * @api {GET} /api/case/getById 根据id查找案例
     * @apiName 根据id查找案例
     * @apiGroup 案例模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {Number} id 案例id
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 2,      //案例id
                "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg", //案例封面图
                "name": "case2",    //案例名
                "sketch": "sketch2",    //案例描述
                "content": "content2",  //案例内容
                "projectId": 1,     //项目id
                "createdAt": "2021-11-11T04:04:05.000Z",
                "updatedAt": "2021-11-11T04:08:50.000Z"
            }
        }
     * 
     * 
     */
  async getById() {
    const { app, ctx, service } = this;
    let checkRes = new ctx.helper.checkData(this, 'id').checkDataRes;
    //如果有缺少参数
    try {
      if (checkRes) {
        let res = await service.web.case.getById();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      return ctx.helper.errorModule(error.message);
    }
  }
}
module.exports = CaseController;
